import { _decorator, Component, JsonAsset, resources } from 'cc';
const { ccclass, property } = _decorator;

// Type definitions for configuration data
export interface WeaponConfig {
    id: string;
    name: string;
    type: string;
    attackRange: number;
    fireRate: number;
    damage: number;
    bulletSpeed?: number;
    bulletPrefab?: string;
    rotateSpeed?: number;
    upgradeOptions?: {
        level: number;
        damage: number;
        fireRate: number;
        attackRange: number;
        extraEffects?: string[];
    }[];
}

export interface EnemyConfig {
    id: string;
    name: string;
    type: string;
    health: number;
    damage: number;
    moveSpeed: number;
    attackRange: number;
    attackSpeed: number;
    dropItems: {
        type: string;
        min?: number;
        max?: number;
        chance: number;
    }[];
}

export interface LevelConfig {
    level: number;
    expRequired: number;
    enemySpawnRate: number;
    enemyTypes: string[];
    maxEnemies: number;
    waves?: {
        time: number;
        enemyTypes: string[];
        count: number;
    }[];
}

export interface ItemConfig {
    id: string;
    name: string;
    type: string;
    value: number;
    duration?: number;
    effect?: string;
}

@ccclass('DataManager')
export class DataManager extends Component {
    private static _instance: DataManager = null;
    
    // Configuration data containers
    private _weaponConfigs: Map<string, WeaponConfig> = new Map();
    private _enemyConfigs: Map<string, EnemyConfig> = new Map();
    private _levelConfigs: Map<number, LevelConfig> = new Map();
    private _itemConfigs: Map<string, ItemConfig> = new Map();
    
    private _isDataLoaded: boolean = false;
    
    // Singleton pattern
    public static get instance(): DataManager {
        return this._instance;
    }
    
    onLoad() {
        // Establish singleton
        if (DataManager._instance === null) {
            DataManager._instance = this;
            this.node.parent.addChild(this.node);
        } else {
            this.node.destroy();
            return;
        }
        
        // Load all configuration data
        this.loadAllData();
    }
    
    private async loadAllData(): Promise<void> {
        // TODO: Load data from resources in production
        // For now, we'll use mock data for development
        this.loadMockData();
        this._isDataLoaded = true;
    }
    
    private loadMockData(): void {
        // Mock weapon configs
        this._weaponConfigs.set('knife', {
            id: 'knife',
            name: 'Knife',
            type: 'melee',
            attackRange: 2,
            fireRate: 1.5,
            damage: 10,
            upgradeOptions: [
                { level: 2, damage: 15, fireRate: 1.7, attackRange: 2.2 },
                { level: 3, damage: 20, fireRate: 2.0, attackRange: 2.5 },
            ]
        });
        
        this._weaponConfigs.set('gun', {
            id: 'gun',
            name: 'Pistol',
            type: 'ranged',
            attackRange: 8,
            fireRate: 1,
            damage: 15,
            bulletSpeed: 20,
            bulletPrefab: 'Bullet',
            upgradeOptions: [
                { level: 2, damage: 20, fireRate: 1.2, attackRange: 9 },
                { level: 3, damage: 25, fireRate: 1.5, attackRange: 10 },
            ]
        });
        
        // Mock enemy configs
        this._enemyConfigs.set('zombie', {
            id: 'zombie',
            name: 'Zombie',
            type: 'melee',
            health: 50,
            damage: 10,
            moveSpeed: 1.5,
            attackRange: 1,
            attackSpeed: 1,
            dropItems: [
                { type: 'exp', min: 1, max: 3, chance: 0.8 },
                { type: 'diamond', chance: 0.1 }
            ]
        });
        
        // Mock level configs
        this._levelConfigs.set(1, {
            level: 1,
            expRequired: 100,
            enemySpawnRate: 2,
            enemyTypes: ['zombie'],
            maxEnemies: 10
        });
        
        this._levelConfigs.set(2, {
            level: 2,
            expRequired: 250,
            enemySpawnRate: 2.5,
            enemyTypes: ['zombie', 'runner'],
            maxEnemies: 15
        });
        
        // Mock item configs
        this._itemConfigs.set('exp', {
            id: 'exp',
            name: 'Experience',
            type: 'resource',
            value: 1
        });
        
        this._itemConfigs.set('diamond', {
            id: 'diamond',
            name: 'Diamond',
            type: 'currency',
            value: 5
        });
    }
    
    public getWeaponConfig(weaponId: string): WeaponConfig | null {
        return this._weaponConfigs.get(weaponId) || null;
    }
    
    public getAllWeaponConfigs(): WeaponConfig[] {
        return Array.from(this._weaponConfigs.values());
    }
    
    public getEnemyConfig(enemyId: string): EnemyConfig | null {
        return this._enemyConfigs.get(enemyId) || null;
    }
    
    public getAllEnemyConfigs(): EnemyConfig[] {
        return Array.from(this._enemyConfigs.values());
    }
    
    public getLevelConfig(level: number): LevelConfig | null {
        return this._levelConfigs.get(level) || null;
    }
    
    public getItemConfig(itemId: string): ItemConfig | null {
        return this._itemConfigs.get(itemId) || null;
    }
    
    public isDataLoaded(): boolean {
        return this._isDataLoaded;
    }
} 